package com.bestvike.stone.spring.shiro.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.AdviceSignature;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * 执行前方法调用点描述
 */
public final class BeforeAdviceMethodInvocation implements MethodInvocation {
    private Object object;
    private Method method;
    private Object[] arguments;

    /**
     * 构造函数
     *
     * @param obj       对象
     * @param method    方法
     * @param arguments 参数
     */
    public BeforeAdviceMethodInvocation(Object obj, Method method, Object[] arguments) {
        this.object = obj;
        this.method = method;
        this.arguments = arguments;
    }

    /**
     * 创建方法调用点实例
     *
     * @param joinPoint 切点
     * @return 调用点描述实例
     */
    public static BeforeAdviceMethodInvocation createFrom(JoinPoint joinPoint) {
        final Signature signature = joinPoint.getSignature();
        if (signature instanceof MethodSignature)
            return new BeforeAdviceMethodInvocation(joinPoint.getThis(), ((MethodSignature) signature).getMethod(), joinPoint.getArgs());
        if (signature instanceof AdviceSignature)
            return new BeforeAdviceMethodInvocation(joinPoint.getThis(), ((AdviceSignature) signature).getAdvice(), joinPoint.getArgs());
        throw new IllegalArgumentException("The joint point signature is invalid: expected a MethodSignature or an AdviceSignature but was " + signature);
    }

    /**
     * 对象
     */
    @Override
    public Object getThis() {
        return this.object;
    }

    /**
     * 方法
     */
    @Override
    public Method getMethod() {
        return this.method;
    }

    /**
     * 参数
     */
    @Override
    public Object[] getArguments() {
        return this.arguments;
    }

    /**
     * 执行方法
     *
     * @return 返回值
     */
    @Override
    public Object proceed() {
        // Do nothing since this adapts a before advice
        return null;
    }
}
